
<!-- 报警明细 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="v.name" :prop="v.prop" :style="{ width: v.width }" v-for="v in searchItems">
        <el-input v-if="v.type == 'input'" v-model="queryParams[v.prop]" :placeholder="'请输入' + v.name" clearable
          @keyup.enter="handleQuery" />
        <el-tree-select v-else-if="v.type == 'treeSelect'" v-model="queryParams[v.prop]" :placeholder="'请选择' + v.name"
          clearable :data="sel_option[v.prop]" :expand-on-click-node="false" :render-after-expand="false"
          @change="handleQuery" check-strictly :props="{ label: 'label' }" node-key="id" filterable />
        <el-date-picker v-else-if="v.type == 'dateTime'" v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
          new Date(2000, 2, 1, 23, 59, 59),]" range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间"
          :disabled-date="disabledDate" @calendar-change="handleChange"></el-date-picker>
        <el-date-picker v-else-if="v.type == 'date'" v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),]" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
        <el-select v-else-if="v.type == 'select'" v-model="queryParams[v.prop]" clearable filterable
          :placeholder="'请输入' + v.name" @change="handleQuery">
          <el-option v-for="item in sel_option[v.prop]" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['statisticalReportForm:alarmDetail:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table highlight-current-row v-loading="loading" :data="tableList">
      <template v-for="v in tableItems">
        <el-table-column :label="v.name" align="left" :prop="v.prop" :width="v.width" show-overflow-tooltip />
      </template>
      <el-table-column label="操作" align="left" class-name="small-padding" :width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)"
            v-hasPermi="['statisticalReportForm:alarmDetail:track']">查看轨迹</el-button>
          <el-button link type="primary" @click="handleFile(scope.row)"
            v-hasPermi="['statisticalReportForm:alarmDetail:file']" v-if="scope.row.amc">证据</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="证据" v-model="fileOpen" width="906px" :destroy-on-close="true">
      <div style="height:500px;background:black">
        <video :src="fileList.video" controls autoplay muted style="width:73.8%;height:500px;float:left"></video>
        <div class="img-dom">
          <img :src="fileList.image[0]" v-if="fileList.image[0]">
          <div class="noImage" v-else>
            <span>暂无图片</span>
          </div>
        </div>
        <div class="img-dom">
          <img :src="fileList.image[1]" v-if="fileList.image[1]">
          <div class="noImage" v-else>
            <span>暂无图片</span>
          </div>
        </div>
        <div class="img-dom">
          <img :src="fileList.image[2]" v-if="fileList.image[2]">
          <div class="noImage" v-else>
            <span>暂无图片</span>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="AlarmDetail">
import dayjs from "dayjs"
import { reactive, toRefs, ref, getCurrentInstance, onActivated } from "vue"
import { treeDept } from "../../api/system/dept";
import request from '../../utils/request'
import useAppStore from '../../store/modules/app'
const appStore = useAppStore();
const loading = ref(false);
const tableList = ref([]);
const { proxy } = getCurrentInstance();
const tableItems = ref([
  { name: '设备名称', prop: "numberPlate" },
  { name: '所属公司', prop: "companyName", width: 200 },
  { name: '报警类型', prop: "name", width: 200 },
  { name: '报警等级', prop: "levelName" },
  { name: '速度(km/h)', prop: "speed", width: 110 },
  { name: '报警时长(min)', prop: "duration", width: 160 },
  { name: '报警开始时间', prop: "time", width: 160 },
  { name: '报警结束时间', prop: "endTime", width: 160 },
  { name: '报警次数', prop: "times" },
  { name: '报警来源', prop: "sourceName" },
  { name: '规则名称', prop: "ruleName" },
]);
const searchItems = ref([
  { name: '设备名称', prop: "numberPlate", type: 'input' },
  { name: '所属公司', prop: "companyId", type: 'treeSelect' },
  { name: '统计时间', prop: "time", type: "dateTime", width: '428px' },//这里定义了time，但是用的是dateRange
  { name: '报警类型', prop: "type", type: "select" },
  { name: '报警来源', prop: "source", type: "select" },
  { name: '报警等级', prop: "level", type: "select" },
]);
let selectData = ''

const total = ref(0);
const showSearch = ref(true);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    companyId: "",
    numberPlate: "",
    type: "",
    level: "",
    source: "",
  },
});
const dateRange = ref([dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")]);

// 证据
const fileOpen = ref(false)
const fileList = ref({
  video: '',
  image: [],
})

//@ts-ignore
const { alarmLevel, source } = proxy.useDict("alarmLevel", "source");
const sel_option: any = ref({ level: alarmLevel, source })
import { getAlarmTypeAll } from "../../api/alarmProcess/alarmProcess";
getAlarmTypeAll().then(resp => {
  sel_option.value.type = resp.data.map(p => {
    return { label: p.name, value: p.type, }
  })
})
treeDept().then(res => {
  sel_option.value.companyId = res.data
})
const { queryParams } = toRefs(data);

function handleChange(val) {
  selectData = val
  if (val[1]) selectData = ''
}
function disabledDate(time) {
  // 是否限制的判断条件
  if (selectData) {
    // 这里就是限制的关键，超过三天禁选
    let day1 = dayjs(dayjs(selectData[0]).add(-2, 'day').format('YYYY-MM-DD HH:mm:ss')).unix();
    let day2 = dayjs(dayjs(selectData[0]).add(2, 'day').format('YYYY-MM-DD HH:mm:ss')).unix();
    return time.getTime() / 1000 > day2 || time.getTime() / 1000 < day1
  } else {
    return false
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  //@ts-ignore
  // proxy.resetForm("queryRef");
  queryParams.value= {
    pageNum: 1,
    pageSize: 15,
    companyId: "",
    numberPlate: "",
    type: "",
    level: "",
    source: "",
  }
  dateRange.value = [dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")];
  handleQuery();
}
/** 查询列表 */
function getList() {
  loading.value = true;
  request({
    url: '/comlbs/reportStatistics/deviceAlarm/detailList',
    method: 'post',
    //@ts-ignore
    params: proxy.addDateRange2(queryParams.value, dateRange.value)
  }).then(({ data }: any) => {
    tableList.value = data.list;
    total.value = data.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  })
}

/** 详情按钮操作 */
function handleDetail(obj: any) {
  let queryTime = [dayjs(obj.time).add(-5, "minutes").format("YYYY-MM-DD HH:mm:ss"), dayjs(obj.endTime).add(5, "minutes").format("YYYY-MM-DD HH:mm:ss")]
  window.open(window.location.origin + "/trajectoryTrack?numberPlate=" + obj.numberPlate + "&deviceNo=" + obj.deviceId + "&companyId=" + obj.companyId + "&queryTime=" + queryTime)
}

/** 导出按钮操作 */
function handleExport() {
  //@ts-ignore
  proxy.download('comlbs/reportStatistics/deviceAlarm/detailListExport',
    //@ts-ignore
    proxy.addDateRange2(queryParams.value, dateRange.value)
    , `报警明细_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}

function handlePageQuery(firstIn: Boolean) {
  // 第一次进入或者参数改变需要查询表格
  let query = appStore.pageQuery;
  if (query) {
    dateRange.value = query.dateRange;
    queryParams.value.numberPlate = query.numberPlate;
    queryParams.value.companyId = query.companyId;
    queryParams.value.type = query.type;
    queryParams.value.source = query.source;
    queryParams.value.level = query.level;
    appStore.clearPageQuery();
    firstIn = true;
  }
  if (firstIn) {
    getList();
  }
}

function handleFile(row) {
  fileOpen.value = true;
  fileList.value.image = []
  request({
    url: "/comlbs/reportStatistics/deviceAlarm/evidence",
    method: "post",
    params: {
      alarmCode: row.amc,
    }
  }).then((result: any) => {
    if (result.code == 200) {
      result.data.forEach((el) => {
        if (el.fte == 2) {
          //@ts-ignore
          fileList.value.video = import.meta.env.VITE_APP_BASE_API + el.pth
        } else {
          //@ts-ignore
          fileList.value.image.push(import.meta.env.VITE_APP_BASE_API + el.pth)
        }
      })
    }
  })
}

onActivated(() => {
  handlePageQuery(false);
})

handlePageQuery(true);
</script>
<style lang='scss' scoped>
.img-dom {
  float: right;
  border: 1px solid black;
  height: 165px;
  width: 224px;

  .noImage {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &:first-of-type {
    border-bottom: 0;
  }

  &:last-of-type {
    border-top: 0;
  }

  img {
    width: 100%;
    height: 100%;
  }
}
</style>